package com.meilele.controller.czh;

import com.meilele.biz.czh.CompanyBiz;
import com.meilele.biz.czh.FiveDesignBiz;
import com.meilele.entity.czh.FiveCompany;
import com.meilele.entity.czh.FiveDesign;
import com.meilele.entity.czh.vo.FiveCDVo;
import com.meilele.util.czh.Constants;
import com.meilele.util.czh.MyApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import java.io.File;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/fiveDesign")
public class FiveDesignController {
    @Resource
    private FiveDesignBiz fiveDesignBiz;
    @Resource
    private CompanyBiz companyBiz;
    @RequestMapping("/list")
    public String listCity(String designCity,int pageNum, Model model){
        byte[] b= new byte[0];//用tomcat的格式（iso-8859-1）方式去读。

        try {
            b = designCity.getBytes("ISO-8859-1");
            String str=new String(b,"utf-8");
            List<FiveDesign> listCity=fiveDesignBiz.findCity(str,pageNum);
            String city=listCity.get(0).getDesignCity();
            int totalCount=fiveDesignBiz.findCount(str);
            double totalPage=totalCount*1.0/ Constants.PAGE_SIZE;
            int total=(int) Math.ceil(totalPage);
            model.addAttribute("city",city);
            model.addAttribute("listCity",listCity);
            model.addAttribute("total",total);
            model.addAttribute("pageNum",pageNum);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        return "/fiveDesign/list";
    }
    @RequestMapping("/detail/{designId}")
    public String detailId(@PathVariable("designId")int designId,Model model){
        FiveDesign fiveDesign=fiveDesignBiz.findId(designId);
        model.addAttribute("fivedesign",fiveDesign);
        return "/fiveDesign/detail";
    }
    @RequestMapping("/details/{designId}")
    public String details(@PathVariable("designId")int designId,Model model) {
        FiveCDVo fiveCDVo=fiveDesignBiz.findCompanyId(designId);
        model.addAttribute("fiveCDVo",fiveCDVo);
        return "/fiveDesign/details";
    }
    @RequestMapping("/perAdd")
    public String preAdd(){
        return "/fiveDesign/add";
    }
    @RequestMapping("/add")
    public String doadd(FiveDesign fiveDesign, MultipartHttpServletRequest request)throws Exception{
        MultipartFile file = request.getFile("photo");
        String fileName = UUID.randomUUID().toString();
        File newFile = new File(Constants.IMAGE_PATH + fileName);
        file.transferTo(newFile);
        FiveDesignBiz fiveDesignBiz= (FiveDesignBiz)MyApplicationContext.getBean("fiveDesignBiz");
        fiveDesign.setDesignLogo(fileName);
        fiveDesignBiz.save(fiveDesign);
        return "redirect:/fiveDesign/preadd";
    }
}
